Tree Fundamentals: Binary Trees

PolyU DSAI2201 Lecture 13 2025-11-25

Binary Trees: Definitions and Properties

A Binary Tree (BT) is a hierarchical data structure where each node has at most two children: a left child and a right child. They are crucial for implementing efficient data storage, search algorithms, and representing computational structures like Abstract Syntax Trees (ASTs).

Key Types & Property

  • Strict/Full Binary Tree: Every non-leaf node has exactly two children.
  • Perfect Binary Tree: A full tree where all leaves are at the same level.

Crucial Node Relationship (Strict Binary Trees):

If NN is the total number of nodes, LL is the number of leaf nodes, and II is the number of internal (non-leaf) nodes:

N=2L1ANDI=L1N = 2L - 1 \quad \text{AND} \quad I = L - 1

If a strict tree has 10 internal nodes (I=10I=10), it must have 11 leaf nodes (L=11L=11), totaling N=21N=21 nodes.

Height and Node Capacity

The height (HH) of a tree (maximum number of edges from the root to the deepest leaf) dictates its maximum capacity:

Level (kk, Root=0)Max NodesTotal Max Nodes (Perfect Tree)
0 (Root)20=12^0 = 11
kk2k2^k2k+112^{k+1} - 1
HH2H2^HNmax=2H+11N_{max} = 2^{H+1} - 1

Optimal Height: For a tree with NN nodes, the minimum possible height (achieved by a balanced tree) is:

Hmin=log2(N+1)1H_{\min} = \lceil \log_2(N+1) \rceil - 1
📝 Checkpoint: Binary Tree Structure

1. A programmer constructs a strict binary tree. If it has 15 non-leaf (internal) nodes, how many total nodes (NN) does it contain?

  • A) 31
  • B) 30
  • C) 16
  • D) 29

2. Which property best describes a Perfect Binary Tree?

  • A) Every node has at most one child.
  • B) It is a full tree where all leaves are at the same depth.
  • C) The tree is skewed to the left.
  • D) Only the root node can have two children.

3. What is the maximum number of nodes in a perfect binary tree with a height of 3?

  • A) 7
  • B) 8
  • C) 15
  • D) 16

4. In a binary tree, what does the 'depth' of a node refer to?

  • A) The longest path from the node to a leaf.
  • B) The total nodes in its subtree.
  • C) The number of edges from the root to that node.
  • D) The number of children the node has.